package com.stylefeng.guns.rest.modular.keyword.controller;

import com.stylefeng.guns.rest.common.CommonResponse;
import com.stylefeng.guns.rest.common.exception.BizExceptionEnum;
import com.stylefeng.guns.rest.common.exception.BussinessException;
import com.stylefeng.guns.rest.common.persistence.model.Keyword;
import com.stylefeng.guns.rest.common.persistence.model.User;
import com.stylefeng.guns.rest.common.utils.TextUtil;
import com.stylefeng.guns.rest.modular.artitle.service.ArticleService;
import com.stylefeng.guns.rest.modular.attent.controller.dto.AttentUserResponse;
import com.stylefeng.guns.rest.modular.attent.service.AttentService;
import com.stylefeng.guns.rest.modular.catalogy.controller.dto.ArticleListResponse;
import com.stylefeng.guns.rest.modular.keyword.controller.dto.AppUser;
import com.stylefeng.guns.rest.modular.keyword.controller.dto.KeywordRequestBody;
import com.stylefeng.guns.rest.modular.keyword.controller.dto.KeywordResponse;
import com.stylefeng.guns.rest.modular.keyword.service.KeywordService;
import com.stylefeng.guns.rest.modular.users.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by ASUS-PC on 2018/6/24.
 */
@RestController
@RequestMapping("/keyword")
public class KeywordController {
    @Autowired
    KeywordService keywordService;
    @Autowired
    AttentService attentService;
    @Autowired
    UserService userService;
    @Autowired
    ArticleService articleService;

    //获取搜索热词列表
    @RequestMapping(value = "/lists" , method = RequestMethod.POST)
    public ResponseEntity<?> getKeywordList(){

        List<Keyword> keyword = keywordService.getSearchKey();
        List<Keyword> keywords = keywordService.getSearchKeywordList();
        return ResponseEntity.ok(CommonResponse.ok("操作成功" , new KeywordResponse(keyword,keywords)));

    }

    @RequestMapping(value = "/searchResult" , method = RequestMethod.POST)
    public ResponseEntity<?> getKeyworResultdList(@RequestBody KeywordRequestBody request){
        if(TextUtil.isEmpty(request.getKeyword())){
            throw new BussinessException(BizExceptionEnum.NEED_PARAMS);
        }
        if(request.getPage()==0){
            request.setPage(1);
        }
        if(request.getSize()==0){
            request.setSize(20);
        }
        //插入关键词表
        boolean isIn = keywordService.searchKeywordIsIn(request.getKeyword());
        if(!isIn){//不存在,插入
            keywordService.insertKeyword(request.getKeyword());
        }else {
            //存在，更新num
            keywordService.updateNum(request.getKeyword());
        }
        if(request.getType()==0){//搜索作者;
           List<AttentUserResponse> userResults = userService.findUsersByKeyword(request);
            if(request.getUserId()>0){
                if(userResults!=null && userResults.size()>0){
                    for (AttentUserResponse entity:userResults) {
                        int isAttent = attentService.queryAttntByUser(request.getUserId(),entity.getId());
                        if(isAttent==1){
                            entity.setIsAttent(1);
                        }else {
                            entity.setIsAttent(0);
                        }
                    }
                }
            }else{
                if(userResults!=null && userResults.size()>0){
                    for (AttentUserResponse entity:userResults) {
                        entity.setIsAttent(0);
                    }
                }
            }
            return ResponseEntity.ok(CommonResponse.ok(userResults));
        }
        if(request.getType()==1){//搜索文章
            List<ArticleListResponse> responses = articleService.findArtitleByKeyword(request);
            return ResponseEntity.ok(CommonResponse.ok(responses));
        }
        return ResponseEntity.ok(CommonResponse.ok(null));
    }

}
